package com.coder.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @BelongsProject:coder_parent
 * @BelongsPackage:com.coder.web.filter
 * @Author:linchong
 * @CreateTime:2019-01-27 21:18
 * @Description:
 */
@Component
public class WebFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //获得request上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //得到request域
        HttpServletRequest request = currentContext.getRequest();
        //得到头信息
        String header = request.getHeader("Authorization");
        if(header!=null && !"".equals(header)){
            //将头信息继续向下传递
            currentContext.addZuulRequestHeader("Authorization",header);

        }


        return null;
    }
}










